﻿
var obj = {
    receivableId: '',
    receivableSerialNumber: '',
    receivableName: '',
    financeTypeName: '',
    endDate: '',
    payer: '',
    payee: '',
    receivableMoney: '',
    practicalMoey: '',
    residueMoney: '',
    status: '',
    paymentNum: '',
    receivableremark: '',
    office_id: '',
    user_id: '',
    createName: '',
    createTime: '',
    updateName: '',
    updateTime: '',
}
var totalCount = 0;
var pageIndex = 1;
var pageSize = 5;

//新增弹框
layui.use('layer', function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;


    // 事件
    util.on('lay-on', {
        'test-page-custom': function () {
            layer.open({
                type: 1,
                title: '新增应收',
                content: `
                      <form class="layui-form" action="">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">流水号</label>
              <div class="layui-input-block">
                <input type="text" name="receivableSerialNumber" id="receivableSerialNumbers" lay-verify="required" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">名称</label>
              <div class="layui-input-inline">
                    <input type="text" name="receivableName" lay-verify="required" id="receivableName" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>

          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">类别：</label>
                <div class="layui-input-block">
                    <select id="financeTypeName" name="financeTypeName" lay-verify="required" lay-filter="mySelectFilter" class="dialog-select"></select>
                </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">最后付款日</label>
              <div class="layui-input-inline">
                    <input type="date" name="endDate" id="endDate" lay-verify="required" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">付款方</label>
              <div class="layui-input-block">
                <input type="text" name="payer" id="payer" lay-verify="required" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">收款方</label>
              <div class="layui-input-inline">
                    <input type="text" name="payee" id="payee" lay-verify="required" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">应收金额</label>
              <div class="layui-input-block">
                <input type="text" name="receivableMoney" lay-verify="required" id="receivableMoney" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">付款次数</label>
              <div class="layui-input-inline">
                    <input type="text" name="paymentNum" lay-verify="required" id="paymentNum" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                      <textarea placeholder="请输入内容" lay-verify="required" name="receivableremark" id="receivableremark" class="layui-textarea"></textarea>
            </div>
          </div>
          <div class="layui-form-item" style="text-align:center">
                <button class="layui-btn" lay-submit lay-filter="demo-login">确认</button>
            <button type="reset" class="layui-btn layui-btn-primary">取消</button>
          </div>
        </form>
                `,
                area: ['700px', '500px'],
                success: function () {
                    
                    initFinanceTypeSelect()
                    // 对弹层中的表单进行初始化渲染
                    form.render();
                    // 表单提交事件
                    form.on('submit(demo-login)', function (data) {

                        var field = data.field;
                        $.ajax({
                            url: '../Receivable/GetReceivableAdd',
                            type: 'post',
                            data: { data: JSON.stringify(field), token: localStorage.getItem("token") },
                            success: function (res) {
                                var s = JSON.parse(res)
                                if (s.code == 1) {
                                    layer.alert("添加成功");
                                    setTimeout(function () {
                                        location.reload();
                                    }, 2000);
                                }
                                else {
                                    layer.alert("添加失败");
                                }
                            }
                        })
                        return false; // 阻止默认 form 跳转
                    });
                }


            });

            // 获取当前日期时间
            var now = new Date();
            var year = now.getFullYear();
            var month = ("0" + (now.getMonth() + 1)).slice(-2);
            var day = ("0" + now.getDate()).slice(-2);
            var hours = ("0" + now.getHours()).slice(-2);
            var minutes = ("0" + now.getMinutes()).slice(-2);
            var seconds = ("0" + now.getSeconds()).slice(-2);
            var serialNumber = year + month + day + hours + minutes + seconds;
            var inputElement = document.getElementById('receivableSerialNumbers');
            inputElement.value = serialNumber;
            console.log(inputElement.value)



        }
    })
    // 查看详情
    window.receivablemodelData = function (id) {
        // 先根据id获取要编辑的数据，发起Ajax请求
        $.ajax({
            type: "GET",
            url: "../Receivable/GetReceivableModel",  // 替换为实际的后台根据id获取数据的接口地址
            data: { receivableId: id, token: localStorage.getItem("token") },
            success: function (res) {
                console.log(res)
                var res = JSON.parse(res)
                console.log(res)
                $(res.data).each(function () {
                    $('#receivableId').val(this.receivableId)
                    $('#receivableSerialNumbers').val(this.receivableSerialNumber)
                    $('#receivableNames').val(this.receivableName)
                    //显示下拉框
                    $("#financeTypeNames").val(this.financeTypeName)

                    //反填日期框
                    var da = new Date(this.endDate);
                    // 获取年、月、日
                    var year = da.getFullYear();
                    var month = (da.getMonth() + 1).toString().padStart(2, '0');  // 月份从0开始，因此加1
                    var day = da.getDate().toString().padStart(2, '0');  // 补充两位数字
                    // 拼接成 "yyyy-MM-dd" 格式
                    var formattedDate = `${year}-${month}-${day}`;
                    $("#endDates").val(formattedDate)
                    $('#payers').val(this.payer)
                    $('#payees').val(this.payee)
                    $('#receivableMoney').val(this.receivableMoney)
                    $('#practicalMoey').val(this.practicalMoey)
                    $('#residueMoney').val(this.residueMoney)
                    $('#status').val(this.status)
                    $('#paymentNum').val(this.paymentNum)
                    $('#receivableremark').val(this.receivableremark)
                    $('#office_id').val(this.office_id)
                    $('#user_id').val(this.user_id)
                    $('#createName').val(this.createName)
                    $('#createTime').val(this.createTime)
                    $('#updateName').val(this.updateName)
                    $('#updateTime').val(this.updateTime)

                    obj.receivableId = this.receivableId
                    obj.receivableSerialNumber = this.receivableSerialNumber
                    obj.receivableName = this.receivableName
                    obj.financeTypeName = this.financeTypeName
                    obj.endDate = this.endDate
                    obj.payer = this.payer
                    obj.payee = this.payee
                    obj.receivableMoney = this.receivableMoney
                    obj.practicalMoey = this.practicalMoey
                    obj.residueMoney = this.residueMoney
                    obj.status = this.status
                    obj.paymentNum = this.paymentNum
                    obj.receivableremark = this.receivableremark
                    obj.office_id = this.office_id
                    obj.user_id = this.user_id
                    obj.createName = this.createName
                    obj.createTime = this.createTime
                    obj.updateName = '张三'
                    obj.updateTime = '2024-11-22'
                })
            },
        });
        initFinanceTypeSelect1()
        layer.open({
            type: 1,
            title: '编辑应收',
            content: `
                      <form class="layui-form" action="" >
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">流水号</label>
              <div class="layui-input-block">
                <input type="text" name="receivableSerialNumber" id="receivableSerialNumbers" disabled autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">名称</label>
              <div class="layui-input-inline">
                    <input type="text" name="receivableName" id="receivableNames" disabled autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>

          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">类别：</label>
                <div class="layui-input-block">
                <input type="text" name="financeTypeNames" id="financeTypeNames" disabled autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">最后付款日</label>
              <div class="layui-input-inline">
                    <input type="date" name="endDate" disabled id="endDates"  class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">付款方</label>
              <div class="layui-input-block">
                <input type="text" name="payer" id="payers" disabled autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">收款方</label>
              <div class="layui-input-inline">
                    <input type="text" name="payee" disabled id="payees" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">应收金额</label>
              <div class="layui-input-block">
                <input type="text" name="receivableMoney" id="receivableMoney" disabled autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">付款次数</label>
              <div class="layui-input-inline">
                    <input type="text" name="paymentNum" id="paymentNum" disabled autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                      <textarea placeholder="请输入内容" name="receivableremark" id="receivableremark" disabled class="layui-textarea"></textarea>
            </div>
          </div>
          <div class="layui-form-item" style="text-align:center">
                <button class="layui-btn" lay-submit lay-filter="demo-login">确认</button>
            <button type="reset" class="layui-btn layui-btn-primary">取消</button>
          </div>
        </form>
                `,
            area: ['700px', '500px'],
            success: function () {
                var obj1 = {
                    receivableId: '',
                    receivableSerialNumber: '',
                    receivableName: '',
                    financeTypeName: '',
                    endDate: '',
                    payer: '',
                    payee: '',
                    receivableMoney: '',
                    practicalMoey: '',
                    residueMoney: '',
                    status: '',
                    paymentNum: '',
                    receivableremark: '',
                    office_id: '',
                    user_id: '',
                    createName: '',
                    createTime: '',
                    updateName: '',
                    updateTime: '',
                };
                // 对弹层中的表单进行初始化渲染
                form.render();
            }
        });
    };

    // 修改
    window.editData = function (id) {
        // 先根据id获取要编辑的数据，发起Ajax请求
        $.ajax({
            type: "GET",
            url: "../Receivable/GetReceivableModel",  // 替换为实际的后台根据id获取数据的接口地址
            data: { receivableId: id, token: localStorage.getItem("token") },
            success: function (res) {
                console.log(res)
                var res = JSON.parse(res)
                console.log(res)
                $(res.data).each(function () {
                    $('#receivableId').val(this.receivableId)
                    $('#receivableSerialNumbers').val(this.receivableSerialNumber)
                    $('#receivableNames').val(this.receivableName)
                    //$('#financeTypeNames').val(this.financeTypeName)
                    //反填日期框
                    var da = new Date(this.endDate);
                    // 获取年、月、日
                    var year = da.getFullYear();
                    var month = (da.getMonth() + 1).toString().padStart(2, '0');  // 月份从0开始，因此加1
                    var day = da.getDate().toString().padStart(2, '0');  // 补充两位数字
                    // 拼接成 "yyyy-MM-dd" 格式
                    var formattedDate = `${year}-${month}-${day}`;
                    $("#endDates").val(formattedDate)
                    $('#payers').val(this.payer)
                    $('#payees').val(this.payee)
                    $('#receivableMoney').val(this.receivableMoney)
                    $('#practicalMoey').val(this.practicalMoey)
                    $('#residueMoney').val(this.residueMoney)
                    $('#status').val(this.status)
                    $('#paymentNum').val(this.paymentNum)
                    $('#receivableremark').val(this.receivableremark)
                    $('#office_id').val(this.office_id)
                    $('#user_id').val(this.user_id)
                    $('#createName').val(this.createName)
                    $('#createTime').val(this.createTime)
                    $('#updateName').val(this.updateName)
                    $('#updateTime').val(this.updateTime)

                    obj.receivableId = this.receivableId
                    obj.receivableSerialNumber = this.receivableSerialNumber
                    obj.receivableName = this.receivableName
                    obj.financeTypeName = this.financeTypeName
                    obj.endDate = this.endDate
                    obj.payer = this.payer
                    obj.payee = this.payee
                    obj.receivableMoney = this.receivableMoney
                    obj.practicalMoey = this.practicalMoey
                    obj.residueMoney = this.residueMoney
                    obj.status = this.status
                    obj.paymentNum = this.paymentNum
                    obj.receivableremark = this.receivableremark
                    obj.office_id = this.office_id
                    obj.user_id = this.user_id
                    obj.createName = this.createName
                    obj.createTime = this.createTime
                    obj.updateName = '张三'
                    obj.updateTime = '2024-11-22'
                })
            },
        });
        initFinanceTypeSelect1()
        layer.open({
            type: 1,
            title: '编辑应收',
            content: `
                      <form class="layui-form" action="">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">流水号</label>
              <div class="layui-input-block">
                <input type="text" name="receivableSerialNumber" id="receivableSerialNumbers" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">名称</label>
              <div class="layui-input-inline">
                    <input type="text" name="receivableName" id="receivableNames" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>

          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">类别：</label>
                <div class="layui-input-block">
                    <select id="financeTypeName" name="financeTypeNames" lay-filter="mySelectFilter" class="dialog-select"></select>
                </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">最后付款日</label>
              <div class="layui-input-inline">
                    <input type="date" name="endDate" id="endDates" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">付款方</label>
              <div class="layui-input-block">
                <input type="text" name="payer" id="payers" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">收款方</label>
              <div class="layui-input-inline">
                    <input type="text" name="payee" id="payees" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">应收金额</label>
              <div class="layui-input-block">
                <input type="text" name="receivableMoney" id="receivableMoney" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">付款次数</label>
              <div class="layui-input-inline">
                    <input type="text" name="paymentNum" id="paymentNum" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                      <textarea placeholder="请输入内容" name="receivableremark" id="receivableremark" class="layui-textarea"></textarea>
            </div>
          </div>
          <div class="layui-form-item" style="text-align:center">
                <button class="layui-btn" lay-submit lay-filter="demo-login">确认</button>
            <button type="reset" class="layui-btn layui-btn-primary">取消</button>
          </div>
        </form>
                `,
            area: ['700px', '500px'],
            success: function () {
                var obj1 = {
                    receivableId: '',
                    receivableSerialNumber: '',
                    receivableName: '',
                    financeTypeName: '',
                    endDate: '',
                    payer: '',
                    payee: '',
                    receivableMoney: '',
                    practicalMoey: '',
                    residueMoney: '',
                    status: '',
                    paymentNum: '',
                    receivableremark: '',
                    office_id: '',
                    user_id: '',
                    createName: '',
                    createTime: '',
                    updateName: '',
                    updateTime: '',
                };
                // 对弹层中的表单进行初始化渲染
                form.render();
                // 表单提交事件
                form.on('submit(demo-login)', function (data) {
                    var field = data.field;
                    obj1.receivableId = obj.receivableId
                    obj1.receivableSerialNumber = field.receivableSerialNumber
                    obj1.receivableName = field.receivableName
                    obj1.financeTypeName = field.financeTypeNames
                    obj1.endDate = field.endDate
                    obj1.payer = field.payer
                    obj1.payee = field.payee
                    obj1.receivableMoney = field.receivableMoney
                    obj1.practicalMoey = obj.practicalMoey
                    obj1.residueMoney = obj.residueMoney
                    obj1.status = obj.status
                    obj1.paymentNum = field.paymentNum
                    obj1.receivableremark = field.receivableremark
                    obj1.office_id = obj.office_id
                    obj1.user_id = obj.user_id
                    obj1.createName = obj.createName
                    obj1.createTime = obj.createTime
                    obj1.updateName = '张三'
                    obj1.updateTime = '2024-11-22'
                    $.ajax({
                        url: '../Receivable/GetReceivableUpd',
                        type: 'post',
                        data: { obj1, token: localStorage.getItem("token") },
                        success: function (res) {
                            var res = JSON.parse(res)
                            console.log(field)
                            console.log(res)
                            if (res.code == 1) {
                                layer.alert("修改成功");
                                setTimeout(function () {
                                    location.reload();
                                }, 2000);
                            }
                            else {
                                layer.alert("修改失败");
                            }
                        }
                    })
                    return false; // 阻止默认 form 跳转
                });
            }
        });
    };

    // 点击上方编辑按钮的事件处理
    $('#batchUpdBtn').click(function () {
        var checkStatus = table.checkStatus('receivabletable');
        var selectedIds = checkStatus.data.map(function (item) { return item.receivableId; });
        console.log(selectedIds)
        if (selectedIds.length === 0) {
            layer.msg('请选择一条要编辑的数据', { icon: 2 });
            return;
        }
        editData(selectedIds[0])
    });


    // 点击批量删除按钮的事件处理
    $('#batchDeleteBtn').click(function () {
        var checkStatus = table.checkStatus('receivabletable');
        var selectedIds = checkStatus.data.map(function (item) { return item.receivableId; });
        console.log(selectedIds)
        if (selectedIds.length === 0) {
            layer.msg('请至少选择一条要删除的数据', { icon: 2 });
            return;
        }
        layer.confirm('确定要批量删除选中的 ' + selectedIds.length + ' 条数据吗？', function (index) {
            // 发送Ajax请求到后端，传递选中的id数组进行删除操作
            $.ajax({
                url: '../Carrier/GetCarrierDelRange', // 后端批量删除接口地址，根据实际调整
                type: 'POST',
                data: { ids: JSON.stringify(selectedIds), token: localStorage.getItem("token") },
                success: function (res) {
                    var res = JSON.parse(res)
                    console.log(res)
                    if (res.code > 0) {
                        layer.msg('批量删除成功', { icon: 1 });
                        Show()
                    } else {
                        layer.msg('批量删除失败：' + res.message, { icon: 2 });
                    }
                    layer.close(index);
                },
                error: function () {
                    layer.msg('请求出错，请稍后再试', { icon: 2 });
                    layer.close(index);
                }
            });
        });
    });

    // 审批
    window.auditData = function (id) {
        // 先根据id获取要编辑的数据，发起Ajax请求
        $.ajax({
            type: "GET",
            url: "../Receivable/GetReceivableModel",  // 替换为实际的后台根据id获取数据的接口地址
            data: { receivableId: id, token: localStorage.getItem("token") },
            success: function (res) {
                console.log(res)
                var res = JSON.parse(res)
                console.log(res)
                $(res.data).each(function () {
                    $('#receivableId').val(this.receivableId)
                    $('#receivableSerialNumbers').val(this.receivableSerialNumber)
                    $('#receivableNames').val(this.receivableName)
                    $('#financeTypeNames').val(this.financeTypeName)
                    //反填日期框
                    var da = new Date(this.endDate);
                    // 获取年、月、日
                    var year = da.getFullYear();
                    var month = (da.getMonth() + 1).toString().padStart(2, '0');  // 月份从0开始，因此加1
                    var day = da.getDate().toString().padStart(2, '0');  // 补充两位数字
                    // 拼接成 "yyyy-MM-dd" 格式
                    var formattedDate = `${year}-${month}-${day}`;
                    $("#endDates").val(formattedDate)
                    $('#payers').val(this.payer)
                    $('#payees').val(this.payee)
                    $('#receivableMoney').val(this.receivableMoney)
                    $('#practicalMoey').val(this.practicalMoey)
                    $('#residueMoney').val(this.residueMoney)
                    $('#status').val(this.status)
                    $('#paymentNum').val(this.paymentNum)
                    $('#receivableremark').val(this.receivableremark)
                    $('#office_id').val(this.office_id)
                    $('#user_id').val(this.user_id)
                    $('#createName').val(this.createName)
                    $('#createTime').val(this.createTime)
                    $('#updateName').val(this.updateName)
                    $('#updateTime').val(this.updateTime)

                    obj.receivableId = this.receivableId
                    obj.receivableSerialNumber = this.receivableSerialNumber
                    obj.receivableName = this.receivableName
                    obj.financeTypeName = this.financeTypeName
                    obj.endDate = this.endDate
                    obj.payer = this.payer
                    obj.payee = this.payee
                    obj.receivableMoney = this.receivableMoney
                    obj.practicalMoey = this.practicalMoey
                    obj.residueMoney = this.residueMoney
                    obj.status = this.status
                    obj.paymentNum = this.paymentNum
                    obj.receivableremark = this.receivableremark
                    obj.office_id = this.office_id
                    obj.user_id = this.user_id
                    obj.createName = this.createName
                    obj.createTime = this.createTime
                    obj.updateName = '张三'
                    obj.updateTime = '2024-11-22'
                })
            },
        });
        initFinanceTypeSelect()
        layer.open({
            type: 1,
            title: '应收审批',
            content: `
                      <form class="layui-form" action="">
                  <form class="layui-form" action="" >
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">流水号</label>
              <div class="layui-input-block">
                <input type="text" name="receivableSerialNumber" id="receivableSerialNumbers" disabled autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">名称</label>
              <div class="layui-input-inline">
                    <input type="text" name="receivableName" id="receivableNames" disabled autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>

          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">类别：</label>
                <div class="layui-input-block">
                <input type="text" name="financeTypeNames" id="financeTypeNames" disabled autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">最后付款日</label>
              <div class="layui-input-inline">
                    <input type="date" name="endDate" disabled id="endDates"  class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">付款方</label>
              <div class="layui-input-block">
                <input type="text" name="payer" id="payers" disabled autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">收款方</label>
              <div class="layui-input-inline">
                    <input type="text" name="payee" disabled id="payees" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">应收金额</label>
              <div class="layui-input-block">
                <input type="text" name="receivableMoney" id="receivableMoney" disabled autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">付款次数</label>
              <div class="layui-input-inline">
                    <input type="text" name="paymentNum" id="paymentNum" disabled autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                      <textarea placeholder="请输入内容" name="receivableremark" id="receivableremark" disabled class="layui-textarea"></textarea>
            </div>
          </div>
          <div class="layui-form-item" style="text-align:center">
                <button class="layui-btn" lay-submit lay-filter="demo-login">审核通过</button>
                <button class="layui-btn" lay-submit lay-filter="demo-false">审核不通过</button>
            <button type="reset" class="layui-btn layui-btn-primary">取消</button>
          </div>
        </form>
                `,
            area: ['700px', '500px'],
            success: function () {
                var obj1 = {
                    receivableId: '',
                    status: '',
                };
                // 对弹层中的表单进行初始化渲染
                form.render();
                // 审核通过
                form.on('submit(demo-login)', function (data) {
                    var field = data.field;
                    obj1.receivableId = obj.receivableId
                    obj1.status = 2
                    $.ajax({
                        url: '../Receivable/GetReceivableAudit',
                        type: 'post',
                        data: { obj1, token: localStorage.getItem("token") },
                        success: function (res) {
                            var res = JSON.parse(res)
                            console.log(field)
                            console.log(res)
                            if (res.code == 1) {
                                layer.alert("审批成功");
                                setTimeout(function () {
                                    location.reload();
                                }, 2000);
                            }
                            else {
                                layer.alert("操作失败");
                            }
                        }
                    })
                    return false; // 阻止默认 form 跳转
                });
                // 审核不通过
                form.on('submit(demo-false)', function (data) {
                    var field = data.field;
                    obj1.receivableId = obj.receivableId
                    obj1.status = 3
                    $.ajax({
                        url: '../Receivable/GetReceivableAudit',
                        type: 'post',
                        data: { obj1, token: localStorage.getItem("token") },
                        success: function (res) {
                            var res = JSON.parse(res)
                            console.log(field)
                            console.log(res)
                            if (res.code == 1) {
                                layer.alert("审核不通过");
                                setTimeout(function () {
                                    location.reload();
                                }, 2000);
                            }
                            else {
                                layer.alert("操作失败");
                            }
                        }
                    })
                    return false; // 阻止默认 form 跳转
                });
            }
        });
    };


    // 实收
    window.payData = function (id) {
        // 先根据id获取要编辑的数据，发起Ajax请求
        $.ajax({
            type: "GET",
            url: "../Receivable/GetReceivableModel",  // 替换为实际的后台根据id获取数据的接口地址
            data: { receivableId: id, token: localStorage.getItem("token") },
            success: function (res) {
                var res = JSON.parse(res)
                $(res.data).each(function () {
                    $('#receivableId').val(this.receivableId)
                    $('#receivableSerialNumbers').val(this.receivableSerialNumber)

                    obj.receivableId = this.receivableId
                    obj.receivableSerialNumber = this.receivableSerialNumber
                })
            },
        });
        initFinanceTypeSelect()
        layer.open({
            type: 1,
            title: '新增实收',
            content: `
                      <form class="layui-form" action="">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">流水号</label>
              <div class="layui-input-block">
                <input type="text" name="receivableSerialNumber" id="receivableSerialNumbers" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">付款机构</label>
              <div class="layui-input-inline">
                    <input type="text" name="payerDepartment" id="payerDepartments" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>

          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">付款方式</label>
                <div class="layui-input-block">
                    <select id="financeTypeName" name="financeTypeName" lay-filter="mySelectFilter" class="dialog-select"></select>
                </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">最后付款日</label>
              <div class="layui-input-inline">
                    <input type="date" name="endDate" id="endDates" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">付款金额</label>
              <div class="layui-input-block">
                <input type="text" name="payMoney" id="payMoney" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>


          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                      <textarea placeholder="请输入内容" name="receivableremark" id="receivableremark" class="layui-textarea"></textarea>
            </div>
          </div>
          <div class="layui-form-item" style="text-align:center">
                <button class="layui-btn" lay-submit lay-filter="demo-login">确认</button>
            <button type="reset" class="layui-btn layui-btn-primary">取消</button>
          </div>
        </form>
                `,
            area: ['700px', '500px'],
            success: function () {

                initFinanceTypeSelect()
                // 对弹层中的表单进行初始化渲染
                form.render();
                // 表单提交事件
                form.on('submit(demo-login)', function (data) {

                    var field = data.field;
                    console.log(JSON.stringify(field))
                    $.ajax({
                        url: '../Receivable/GetAuditAdd',
                        type: 'post',
                        data: { data: JSON.stringify(field), token: localStorage.getItem("token") },
                        success: function (res) {
                            console.log(res)
                            var s = JSON.parse(res)
                            if (s.code == 0) {
                                layer.alert("添加成功");
                                setTimeout(function () {
                                    location.reload();
                                }, 2000);
                            }
                            else {
                                layer.alert("添加失败");
                            }
                        }
                    })
                    return false; // 阻止默认 form 跳转
                });
            }
        });
    };

   
    // 定义删除数据的函数
    window.deleteData = function (id) {
        layer.confirm('确定要删除该条数据吗？', function (index) {
            $.ajax({
                type: "Post",
                url: "../Receivable/GetReceivableDel",  // 替换为实际的后台删除数据接口地址
                data: { data: JSON.stringify({ receivableId: id }), token: localStorage.getItem("token") },
                success: function (res) {
                    console.log(res)
                    var res = JSON.parse(res)
                    if (res.code === 1) {
                        layer.alert("删除成功");
                        Show();  // 重新加载数据，刷新表格显示
                    } else {
                        layer.alert("删除失败");

                    }
                },
                error: function () {
                    layer.msg('删除失败，请稍后重试', {
                        time: 2000,
                        icon: 2
                    });
                }
            });
            layer.close(index);
        });
    };


   
})
$(function () {
    initFinanceTypeSelect()
    Show()
})
function refesh() {
    location.reload();
}
//显示
function Show() {
    layui.use('table', function () {
        var form = layui.form;
        var table = layui.table;

        var receivableSerialNumber = $("#receivableSerialNumber").val();
        var receivableName = $("#receivableName").val();
        var financeTypeName = $("#financeTypeName").val();
        var payer = $("#payer").val();
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        var status = $("#status").val();
        $.ajax({
            url: '../Receivable/GetReceivableShow', // 后端接口地址，需替换为实际地址
            type: 'get',
            data: {
                receivableSerialNumber: receivableSerialNumber,
                receivableName: receivableName,
                financeTypeName: financeTypeName,
                payer: payer,
                startTime: startTime,
                endTime: endTime,
                status: status || 0,
                pageIndex: 1,
                pageSize: 100,
                token: localStorage.getItem("token"),
            },
            success: function (res) {
                res = JSON.parse(res)
                // res 是从后端获取到的 JSON 数据
                if (res.code === 0) {
                    console.log(res.data.list);
                    table.render({
                        elem: receivabletable, // 对应 HTML 中表格的 id
                        data: res.data.list, // 绑定获取到的数据
                        page: true,
                        limit: 3,
                        limits: [3, 5, 8],
                        toolbar: '#toolbarDemo',
                        defaultToolbar: ['exports'],
                        cols: [[
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'receivableSerialNumber', title: '流水号' },
                            { field: 'receivableName', title: '名称' },
                            { field: 'financeTypeName', title: '分类' },
                            { field: 'payer', title: '付款方' },
                            { field: 'receivableMoney', title: '应收金额' },
                            { field: 'practicalMoey', title: '实收金额' },
                            {
                                field: 'residueMoney', title: '剩余金额', templet: function (d) {
                                    return d.receivableMoney - d.practicalMoey;
                                }
                            },
                            { field: 'statusText', title: '状态' },
                            {
                                field: 'createTime', title: '创建时间', templet: function (d) {
                                    return formatTime(d.createTime); // 调用自定义的时间格式转换函数
                                }
                            },
                            {
                                field: '', title: '操作', width: 350, templet: function (d) {
                                    //var buttonHtml = '';
                                    //if (d.statusText === '等待审核' ) {
                                    //    buttonHtml += '<a class="layui-btn layui-btn-edit layui-btn-xs" lay-on="test-page-edit"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                                    //        '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.allocationNo + ',' + d.allocationProductId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>'
                                    //        +
                                    //        '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="deleteData(' + d.allocationNo + ',' + d.allocationProductId + ')"><i class="layui-icon layui-icon-delete"></i>查看</a>'
                                    //        +
                                    //        '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="deleteData(' + d.allocationNo + ',' + d.allocationProductId + ')"><i class="layui-icon layui-icon-delete"></i>收款</a>'
                                    //        +
                                    //        '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="deleteData(' + d.allocationNo + ',' + d.allocationProductId + ')"><i class="layui-icon layui-icon-delete"></i>审核</a>';
                                    //}
                                    //return buttonHtml;


                                    return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.receivableId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                                        '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.receivableId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>'
                                        +
                                        '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="receivablemodelData(' + d.receivableId + ')"><i class="layui-icon layui-icon-search"></i>查看</a>'
                                        +
                                        '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="payData(' + d.receivableId + ')"><i class="layui-icon layui-icon-rmb"></i>收款</a>'
                                        +
                                        '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="auditData(' + d.receivableId + ')"><i class="layui-icon layui-icon-util"></i>审核</a>'; 
                                }
                            }
                        ]]
                    });
                } else {
                    console.log(res.msg);
                }
            },
            error: function () {
                console.log('获取数据失败');
            }
        });
    });
}
// 定义时间格式转换函数
function formatTime(timeStamp) {
    if (timeStamp) {
        var date = new Date(timeStamp);
        var year = date.getFullYear();
        var month = ('0' + (date.getMonth() + 1)).slice(-2);
        var day = ('0' + date.getDate()).slice(-2);
        var hour = ('0' + date.getHours()).slice(-2);
        var minute = ('0' + date.getMinutes()).slice(-2);
        var second = ('0' + date.getSeconds()).slice(-2);
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    }
    return "";
}
//应收状态下拉框
layui.use('form', function () {
    var form = layui.form;
    $.ajax({
        url: "../Receivable/GetReceivableStatusShow",
        type: "get",
        data: { token: localStorage.getItem("token") },
        success: function (data) {
            var obj = JSON.parse(data);
            console.log(obj)
            var options = '<option value="">请选择</option>'; // 添加默认选项
            $.each(obj, function (index, item) {
                options += '<option value="' + item.value + '">' + item.text + '</option>';
            });
            $("#status").html(options);
            form.render('select'); // 重新渲染下拉框
        },
        error: function () {
            console.log("获取数据失败");
        }
    });
});
//财务类型下拉框
function initFinanceTypeSelect() {
    layui.use('form', function () {
        var form = layui.form;
        $.ajax({
            url: "../Purchase/GetFinanceTypeShow",
            type: "get",
            data: { token: localStorage.getItem("token") },
            success: function (data) {
                var obj = JSON.parse(data);
                console.log(obj)
                var options = '<option value="">请选择</option>'; // 添加默认选项
                $.each(obj.data, function (index, item) {
                    options += '<option value="' + item.financeTypeName + '">' + item.financeTypeName + '</option>';
                });
                $(".dialog-select#financeTypeName").html(options);
                form.render('select'); // 重新渲染下拉框
            },
            error: function () {
                console.log("获取数据失败");
            }
        });
    });
}

//财务类型反填下拉框
function initFinanceTypeSelect1() {
    layui.use('form', function () {
        var form = layui.form;
        $.ajax({
            url: "../Purchase/GetFinanceTypeShow",
            type: "get",
            data: { token: localStorage.getItem("token") },
            success: function (data) {
                var obj = JSON.parse(data);
                console.log(obj)
                var options = '<option value="">请选择</option>'; // 添加默认选项
                $.each(obj.data, function (index, item) {
                    //debugger
                    if (item.financeTypeName == obj.data[index].financeTypeName) {
                        options += '<option value="' + item.financeTypeName + '"selected>' + item.financeTypeName + '</option>';
                    }
                    else {
                        options += '<option value="' + item.financeTypeName + '">' + item.financeTypeName + '</option>';

                    }
                });
                $(".dialog-select#financeTypeName").html(options);
                form.render('select'); // 重新渲染下拉框
            },
            error: function () {
                console.log("获取数据失败");
            }
        });
    });
}